<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 11:01
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Lenovo extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword) && $keyword != ''){
                $map['a.lenovo_name'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('lenovo')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('lenovo')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('lenovo_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count,  'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *解冻
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('lenovo')->where('lenovo_id',$task_id)->setField('lenovo_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *冻结
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('lenovo')->where('lenovo_id',$task_id)->setField('lenovo_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     *删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('lenovo')->where('lenovo_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    public function add(){
        if(Request::instance()->isPost()){
            $password = input('param.lenovo_name');
            $data['lenovo_name'] = $password;
            $data['lenovo_time'] = date('Y-m-d H:i:s');
            $res = Db::name('lenovo')->insertGetId($data);
            if($res){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败']);
            }
        }
        return $this->fetch();
    }
    //编辑
    public function update(){
        if(Request::instance()->isPost()){
            $task_id = input('param.id');
            $password = input('param.lenovo_name');
            $res = Db::name('lenovo')->where('lenovo_id',$task_id)->setField('lenovo_name',$password);
            if($res){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败']);
            }
        }
        $id = input('param.lenovo_id');
        $info = Db::name('lenovo')->where('lenovo_id',$id)->value('lenovo_name');
        $this->assign('id',$id);
        $this->assign('info',$info);
        return $this->fetch();
    }
}